package shapewar.net.client;

public class MyStack <T>{
	int i = 0,j = 0;
	T objs[];
	Thread thread;
	private CallBack<T> callback;
	@SuppressWarnings("unchecked")
	public MyStack(int size,CallBack<T> callback){
		objs = (T[]) new Object[size];
		thread = new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					deal();
				}
			}
		});
		this.callback = callback;
		thread.start();
	}
	
	public MyStack(CallBack<T> callback){
		this(10,callback);
	}
	
	public synchronized void put(T obj){
		objs[i++%objs.length] = obj;
		notify();
	}
	
	public synchronized void deal(){
		try {
			wait();
			while(j<i){
				callback.call(objs[j++%objs.length]);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}
